package fi.jamk.ourcookingapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.Toast;
import fi.jamk.ourcookingapp.R;
import fi.jamk.ourcookingapp.adapter.MySortingAdapter;
import fi.jamk.ourcookingapp.backend.architecture.manager.ReceiptManager;
import fi.jamk.ourcookingapp.backend.exceptions.EmptySearchException;

/**
 * This activity is for showing the list for sorting the receipts
 * @author Marius
 */
public class SortingActivity extends ListActivity implements OnScrollListener {

	private List<String> sortingList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setBackgroundDrawableResource(R.drawable.bg);
		sortingList = new ArrayList<String>();
		sortingList.add(ReceiptManager.ORDER_DATE);
		sortingList.add(ReceiptManager.ORDER_COUNT_CLICKS);
		sortingList.add(ReceiptManager.ORDER_DURATION);
		sortingList.add(ReceiptManager.ORDER_RATING);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MySortingAdapter adapter = new MySortingAdapter(this, R.layout.list_item, getResources().getStringArray(R.array.lstSorting));
		setListAdapter(adapter);
		getListView().setOnScrollListener(this);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		try {
			String sort = sortingList.get(position);
			AppActivity.facade.setOption(ReceiptManager.ORDER_BY, sort);
			// choosing the stream tab
			AppActivity.tabHost.setCurrentTab(0);
			finish();
		} catch (EmptySearchException e) {
			Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
		}
		
	}

	@Override
	public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
	}

	@Override
	public void onScrollStateChanged(AbsListView arg0, int arg1) {
	}
	
}
